package main

import (

	"strconv"
	"github.com/boltdb/bolt"
	"fmt"
	"github.com/xuri/excelize"
	"match-spider/util/boltdb"
	"strings"
	"math"
)


/**
澳门盘口为0.25
 */
func checkAsiaAomen(data string) (bool) {
	if !strings.Contains(data,"澳门"){
		return false
	}
	asiaSlice:=strings.Split(data,"!")
	m1:=make(map[string]float64)
	m2:=make(map[string]float64)
	b:=true
	for _,v:=range asiaSlice{
		d:=strings.Split(v,"^")
		c:=d[0]
		f1,_:=strconv.ParseFloat(d[3],64)
		f2,_:=strconv.ParseFloat(d[6],64)

		if f2<(-0.25)||f2>0.25{
			b=false
			break
		}
		m1[c]=f1
		m2[c]=f2
	}

	if !b{
		return false
	}
	s1:=m1["Bet365"]

	for k,v:=range m1 {
		if k!="澳门"&&k!="易胜博"&&v!=s1{
			b=false
			break
		}
	}
	if !b{
		return false
	}
	s2:=m2["Bet365"]

	for k,v:=range m2 {
		if k!="澳门"&&k!="易胜博"&&v!=s2{
			b=false
			break
		}
	}

	if !b{
		return false
	}
	if math.Abs(m1["澳门"]-m2["澳门"])>=0.5||math.Abs(m1["易胜博"]-m2["易胜博"])>=0.5{
		return false
	}
	if m2["澳门"]==m2["易胜博"]&&m2["澳门"]!=s2{
		return true
	}
	return false
}

func scoreAomen(fn func([]interface{},int)){
	i:=0
	boltdb.ScanTx("matchs-2017", func(tx *bolt.Tx) {

		tx.ForEach(func(name []byte, b *bolt.Bucket) error {
			b.ForEach(func(k, v []byte) error {
				content:=string(v)
				s:=strings.Split(content,"^")

				league:=s[1]

				s1,_:=strconv.ParseFloat(s[4],64)
				s2,_:=strconv.ParseFloat(s[6],64)

				boltdb.Get("asia-2017",league,string(k), func(asia string) {
					b1:=checkAsiaAomen(asia)
					if !b1{
						return
					}
					i++
					slice:=[]interface{}{string(k),league,s1,s2}
					fn(slice,i)
				})

				return nil
			})
			return nil
		})

	})
	fmt.Println(i)
}

func main()  {
	e:=[]string{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
	xlsx := excelize.NewFile()

	categories := []string{

	}
	for k,v:=range categories{
		len:=int(k/26)
		s:=""
		if len>0{
			s+=e[len-1]+e[k%26]
		}else{
			s+=e[k%26]
		}

		xlsx.SetCellValue("Sheet1",s+"1", v)
	}
	scoreAomen(func(s []interface{}, i int) {
		for k,v:=range s{
			len:=int(k/26)
			s:=""
			if len>0{
				s+=e[len-1]+e[k%26]
			}else{
				s+=e[k%26]
			}
			xlsx.SetCellValue("Sheet1",s+strconv.Itoa(i+1), v)
		}
	})
	err := xlsx.SaveAs("./Workbook.xlsx")
	if err != nil {
		fmt.Println(err)
	}

}
